var info  = '<div id="sharelinks-crx-popdiv" class="" style="visibility: hidden"><img src="" id="ok"><span id="msg"></span></div>';
var e = document.createElement('div');
e.innerHTML = info;
document.body.appendChild(e);

// 显示收藏操作结果提示信息
function showNotifi(msg, type) {
    var el = document.getElementById('sharelinks-crx-popdiv');

    if (type=='success') {
        // 成功提示
        el = document.getElementById('sharelinks-crx-popdiv');
        el.setAttribute('class','success');
        document.getElementById('msg').innerText=msg;
        document.getElementById('ok').setAttribute('src',chrome.extension.getURL("assets/images/right.png"));

        el.setAttribute('style', 'visibility: visible');

        setTimeout(function () {
            el.setAttribute('style', 'visibility: hidden');
        }, 1500);
    } else if (type=='error') {
        // 错误提示
        // document.body.innerHTML += error;
        el = document.getElementById('sharelinks-crx-popdiv');
        el.setAttribute('class','error');
        document.getElementById('msg').innerText=msg;
        document.getElementById('ok').setAttribute('src',chrome.extension.getURL("assets/images/error.png"));

        el.setAttribute('style', 'visibility: visible;');

        setTimeout(function () {
            el.setAttribute('style', 'visibility: hidden');
        }, 1500);
    } else {
        // 警告提示
        el = document.getElementById('sharelinks-crx-popdiv');
        el.setAttribute('class','warning');
        document.getElementById('msg').innerText=msg;
        document.getElementById('ok').setAttribute('src',chrome.extension.getURL("assets/images/warning.png"));

        el.setAttribute('style', 'visibility: visible;');

        setTimeout(function () {
            el.setAttribute('style', 'visibility: hidden');
        }, 1500);
    }
}

// 接收backend发送过来的消息
chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {

        // 收藏成功
        if (request.action == 'add_to_collection_success' || request.action=='success') {
            showNotifi(request.msg, 'success');
        }

        // 收藏失败
        if (request.action == 'add_to_collection_error' || request.action=='error') {
            showNotifi(request.msg, 'error');
        }

        // 收藏失败
        if (request.action == 'warning') {
            showNotifi(request.msg, 'warning');
        }
    });